<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="站点+小组" prop="name" label-width="80px" size="mini">
        <el-cascader
          v-model="cascaderId"
          :options="cascaderList"
          @change="cascaderChange"
          :props="props"
          :clearable="true"></el-cascader>
      </el-form-item>
      <el-form-item label="订单号" prop="payNo">
        <el-input
          v-model="queryParams.payNo"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" style="height:32px;"  >
        <el-date-picker clearable size="small" style="width: 240px"
                        v-model="dateRange"
                        type="daterange"
                        value-format="yyyy-MM-dd"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        align="right"
                        unlink-panels
                        :picker-options="pickerOptions"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="dataRecovery"
          :loading="readLoad"
          v-hasPermi="['hps:orderRecord:dataRecovery']"
        >数据恢复</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['hps:orderRecord:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hps:orderRecord:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['hps:orderRecord:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="100"/>
      <el-table-column label="站点" align="center" prop="siteName" />
      <el-table-column label="分组" align="center" prop="groupName" />
      <el-table-column label="支付订单号" align="center" prop="payNo" width="250"/>
      <el-table-column label="支付时间" align="center" prop="payTime" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="手机号" align="center" prop="phone" width="120"/>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hps:orderRecord:edit']"
          >详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['hps:orderRecord:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改环鄱赛订单记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="站点" prop="siteName">
              <el-input v-model="form.siteName" placeholder="请输入站点名称" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分组" prop="groupName">
              <el-input v-model="form.groupName" placeholder="请输入分组名称" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="支付宝账号" prop="buyerLogonId">
              <el-input v-model="form.buyerLogonId" placeholder="请输入登录账号" :disabled="disabled"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="订单号" prop="payNo">
              <el-input v-model="form.payNo" placeholder="请输入订单号" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付时间" prop="payTime">
              <el-input v-model="form.payTime" placeholder="请输入支付时间" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="用户姓名" prop="realName">
              <el-input v-model="form.realName" placeholder="请输入用户姓名" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="证件姓名" prop="certName">
              <el-input v-model="form.certName" placeholder="请输入证件姓名" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证件号码" prop="certNo">
              <el-input v-model="form.certNo" placeholder="请输入证件号码" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="车队/俱乐部" prop="club">
              <el-input v-model="form.club" placeholder="请输入车队/俱乐部" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" :style="{width: '100%'}" placeholder="请选择性别" :disabled="disabled">
                <el-option
                  v-for="item in sexOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="紧急联系人" prop="emergencyContact">
              <el-input v-model="form.emergencyContact" placeholder="请输入紧急联系人" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="紧急电话" prop="emergencyPhone">
              <el-input v-model="form.emergencyPhone" placeholder="请输入紧急电话" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateRecord,
  exportRecord,
  dataRecovery
} from "@/api/hps/orderRecord";
import {cascaderSite} from "@/api/hps/site";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表单禁用
      disabled: true,
      readLoad: false,
      // 总条数
      total: 0,
      // 环鄱赛订单记录表格数据
      recordList: [],
      sexOptions: [{
        dictValue: '2',
        dictLabel: "女"
      },{
        dictValue: '1',
        dictLabel: "男"
      },{
        dictValue: '0',
        dictLabel: "未知"
      }],
      statusOptions: [{
        dictValue: '0',
        dictLabel: "未恢复"
      },{
        dictValue: '1',
        dictLabel: "已恢复"
      }],
      cascaderList: [],
      props: {
        value: "id",
        label: "name",
        children: "hpsSiteGroupList",
        checkStrictly: true
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteId: undefined,
        groupId: undefined,
        payNo: undefined,
        wxUserId: undefined,
        certName: undefined,
        certNo: undefined,
        certType: undefined,
        phone: undefined,
        sex: undefined,
        club: undefined,
        emergencyContact: undefined,
        emergencyPhone: undefined,
        wxClientId: undefined,
      },
      dateRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      cascaderId: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    cascaderSite().then(response => {
      this.cascaderList = response.data
      this.cascaderList.forEach(item => {
        item.id = item.id
        if (item.hpsSiteGroupList && item.hpsSiteGroupList.length > 0) {
          item.hpsSiteGroupList.forEach(i => {
            i.id = i.groupId;
            i.name = i.groupName
          })
        } else {
          item.hpsSiteGroupList = undefined
        }
      })
    })
    this.getList();
  },
  methods: {
    /** 查询环鄱赛订单记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        siteId: undefined,
        groupId: undefined,
        payNo: undefined,
        wxUserId: undefined,
        certName: undefined,
        certNo: undefined,
        certType: undefined,
        phone: undefined,
        sex: undefined,
        club: undefined,
        emergencyContact: undefined,
        emergencyPhone: undefined,
        createTime: undefined,
        wxClientId: undefined,
        remark: undefined,
        buyerLogonId: undefined
      };
      this.resetForm("form");
    },
    // 状态字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加环鄱赛订单记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改环鄱赛订单记录";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateRecord(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addRecord(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除环鄱赛订单记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecord(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有环鄱赛订单记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    syncData(ids){
      var that = this;
      this.readLoad = true;
      dataRecovery(ids).then(response => {
        that.getList();
        that.readLoad = false;
        that.$alert(response.msg, "生成结果", { dangerouslyUseHTMLString: true,type: "success" });
      }).catch(function(res) {
        that.getList();
        that.readLoad = false;
      });
    },
    // 数据恢复
    dataRecovery(row){
      var that = this;
      const ids = row.id || this.ids;
      this.$confirm('是否确认恢复环鄱赛订单数据', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function(res) {
        return that.syncData(ids);
      })
    },
    cascaderChange(value) {
      // console.log(value);
      this.cascaderId = value;
      if (value == undefined || value.length == 0) {
        this.queryParams.siteId = undefined;
        this.queryParams.groupId = undefined;
      } else {
        if (value.length == 1) {
          this.queryParams.siteId = value[0]
        }
        if (value.length == 2) {
          this.queryParams.siteId = value[0]
          this.queryParams.groupId = value[1]
        }
      }
    }
  }
};
</script>
